package controllers

import (
	"api01/models"
	"api01/util"
	"github.com/astaxie/beego/validation"
	"time"
)

// Operations about Users
type LoginController struct {
	BaseController
}

// @Title Login
// @Description user login
// @Param	account		query 	string	true		"The account for login"
// @Param	password		query 	string	true		"The password for login"
// @Success 200 {string} login success
// @Failure 403 user not exist
// @router /login [post]
func (this *LoginController) Login() {
	account := this.GetString("account")
	password := this.GetString("password")
	valid := validation.Validation{}
	valid.Required(account, "account").Message("账号不能为空")
	valid.Required(password, "password").Message("密码不能为空")
	if valid.HasErrors() {
		util.OutPutError(401,valid.Errors[0].Message)
	}
	token, err := models.Login(account, password)
	util.CheckError(err)
	util.OutPutSuccess(map[string]string{"token":token})
}

// @Title Register
// @Description create user
// @Param	body		body 	models.User	true		"body for user content"
// @Success 200 {int} models.User.Id
// @Failure 403 body is empty
// @router /register [post]
func (this *LoginController) Register() {
	name := this.GetString("name")
	account := this.GetString("account")
	password := this.GetString("password")

	valid := validation.Validation{}
	valid.Required(account, "account").Message("账号不能为空")
	valid.Min(account, 5,"account").Message("账号长度在5~18位")
	valid.Max(account, 18,"account").Message("账号长度在5~18位")

	valid.Required(password, "password").Message("密码不能为空")
	valid.Min(password, 6,"password").Message("密码长度在6~18位")
	valid.Max(password, 18,"password").Message("密码长度在6~18位")

	if valid.HasErrors() {
		util.OutPutError(401,valid.Errors[0].Message)
	}
	if name == "" {
		name = account
	}
	user := models.User{
		Name:     name,
		Account:  account,
		Password: password,
		Status:   models.USER_STATUS_PASS,
		CreateAt: time.Now().Format("2006-01-02 15:04:05") ,
		UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
	}
	err := models.Register(&user)
	util.CheckError(err)
	util.OutPutSuccess(map[string]string{"message":"注册成功"})
}

// @Title ChangePassword
// @Description create user
// @Param	account		query 	string	true		" login account"
// @Param	password		query 	string	true		"login password"
// @Param	new_password		query 	string	true		"new password"
// @Param	new_password_repeat		query 	string	true		"repeat new password"
// @Success 200 {int} change success
// @Failure 403 change error
// @router /password [post]
func  (this *LoginController) ChangePassword()  {
	account := this.GetString("account")
	password := this.GetString("password")

	newPassword := this.GetString("new_password")
	newPasswordRepeat := this.GetString("new_password_repeat")


	valid := validation.Validation{}
	valid.Required(account, "account").Message("账号不能为空")
	valid.MinSize(account, 5,"account").Message("账号长度在5~18位")
	valid.MaxSize(account, 18,"account").Message("账号长度在5~18位")

	valid.Required(password, "password").Message("密码不能为空")
	valid.MinSize(password, 6,"password").Message("密码长度在6~18位")
	valid.MaxSize(password, 18,"password").Message("密码长度在6~18位")

	valid.Required(newPassword, "password").Message("新密码不能为空")
	valid.MinSize(newPassword, 6,"password").Message("新密码长度在6~18位")
	valid.MaxSize(newPassword, 18,"password").Message("新密码长度在6~18位")

	if newPassword != newPasswordRepeat {
		util.OutPutError(401,"两次输入新密码不一致")
	}

	err := models.ChangePassword(account, password, newPassword)
	util.CheckError(err)
	util.OutPutSuccess(map[string]string{"message":"修改成功"})
}